import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet ("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查看是否已经有了回话
        HttpSession session =req.getSession(false);
        if(session==null){
            resp.getWriter().write("用户未登录");


            /*    ajax发起的请求，如果响应是302，不会触发任何跳转。 需手动跳转                        */
//            resp.setStatus(302);
//            resp.sendRedirect("login.html");

            resp.setStatus(403);

            return;
        }else{

            String getBlogId=req.getParameter("blogId");


            /*    blog_detail.html  用                 */
            if(getBlogId!=null){

                int blogId=Integer.parseInt(getBlogId);
                BlogDao blogDao=new BlogDao();
                try {
                    Blog blog=blogDao.selectOne(blogId);
                    resp.setContentType("application/json;charset=utf8");
                    resp.getWriter().write(objectMapper.writeValueAsString(blog));


                } catch (SQLException e) {
                    e.printStackTrace();
                }


            }else{
                /*   blog_list.html   用                   */
                //已经登录的
                BlogDao blogDao=new BlogDao();
                try {
                    List<Blog> list=blogDao.selectAll();
                    //转为json格式 （传给前端）
                    resp.setContentType("application/json;charset=utf8");
                    resp.getWriter().write(objectMapper.writeValueAsString(list));

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }



        }
    }


    /*  提交博客   */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession(false);
        //获取回话
        if(session==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            resp.setStatus(403);
            return;

        }
        //获取session中保存的当前登录的用户
        User user=(User)session.getAttribute("user");
        if(user==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            resp.setStatus(403);
            return;
        }

        //获取请求中的参数     ------- 以免不能正常解析
        req.setCharacterEncoding("utf8");
        //获取标题
        String title=req.getParameter("title");
        //获取博客内容
        String content=req.getParameter("content");

        Blog blog=new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());

        BlogDao blogDao=new BlogDao();
        try {
            blogDao.insert(blog);
            resp.getWriter().write("博客发布成功");
            //重定向，回到列表页
            resp.setStatus(302);
            resp.sendRedirect("blog_list.html");

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
